import ArrayList from "./ArrayList";
/**
 * 存放画布中框选中的图形
 */
export class Selection {
  constructor() {
    this.all = new ArrayList();
    this.primary = null; //鼠标点击选中图形 用于区分框选图形
  }

  add(figure) {
    if (figure !== null && !this.all.contains(figure)) {
      this.all.add(figure);
    }

    return this;
  }

  remove(figure) {
    this.all.remove(figure);
    return this;
  }

  clear() {
    this.all = new ArrayList();
    return this;
  }

  getSize() {
    return this.all.getSize();
  }

  each(func, reverse) {
    this.all.each(func, reverse);
    return this;
  }

  contains(figure, checkDescendant) {
    if (checkDescendant) {
      for (let i = 0; i < this.all.getSize(); i++) {
        let figureToCheck = this.all.get(i);
        if (figureToCheck === figure || figureToCheck.contains(figure)) {
          return true;
        }
      }
      return false;
    }
    return this.all.contains(figure);
  }

  getAll() {
    return this.all.clone();
  }

  getPrimary() {
    return this.primary;
  }

  setPrimary(figure) {
    this.primary = figure;
  }
}

export default Selection;
